<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

function photosAction() {
    if(isset($_POST['photosActionType'])) {
        $actionType = $_REQUEST['photosActionType'];
        switch ($actionType) {
            case 'addPhotos':
                addPhotos();
                break;
            case 'deletePhotos':
                deletePhotos($_POST['id']);
                break;
            default:
                getAllPhotos();
        }
    }
}

function listDir($dir) {
    if(is_dir($dir)) {
        $arrIgnore = Array('.','..','Thumbs.db');
        if($handle = opendir($dir)) {
            while(($file = readdir($handle)) !== false) {
                $pos = strpos($file, '.jp');
                $pos1 = strpos($file, '.bm');
                $pos2 = strpos($file, '.gi');
                $pos3 = strpos($file, '.pn');
                if(!in_array($file, $arrIgnore) && $pos==FALSE && $pos1==FALSE && $pos2==FALSE && $pos3==FALSE) {
                    echo '<a href="?dir='.$dir.$file.'/&place='.$_GET['place'].'">'.$file.'</a>&nbsp;&nbsp;';
                }
            }
        }
        closedir($handle);
    }
}
function listImages($dir) {
    if(is_dir($dir)) {
        if($handle = opendir($dir)) {
            while(($file = readdir($handle)) !== false) {
                $pos = strpos($file, '.jp');
                if($pos!=FALSE) {
                    if($_GET['place']=='description') {
                        echo '<img onclick="javascript: returnToParent(\'/'.$dir.$file.'\');"  style="border-color:silver;cursor: pointer;" src="/'.$dir.$file.'" alt="'.$file.'"/>'."\n";
                    }else {
                        echo '<img onclick="javascript: returnToParent(\'/'.$dir.$file.'\');"  style="border-color:silver;cursor: pointer;" src="/'.$dir.$file.'" alt="'.$file.'"/>'."\n";
                    }
                }
            }
        }
        closedir($handle);
    }
}

function listImagesDescription($dir,$thumbDir) {
    if(is_dir($dir)) {
        if($handle = opendir($dir)) {
            while(($file = readdir($handle)) !== false) {
                $pos = strpos($file, '.jp');
                if($pos!=FALSE) {
                    if (file_exists($thumbDir.$file)) {
                        echo '<img onclick="javascript: returnToParent(\'/'.$dir.$file.'\');"  style="border-color:silver;cursor: pointer;" src="/'.$thumbDir.$file.'" alt="'.$file.'"/>'."\n";
                    } 
                }
            }
        }
        closedir($handle);
    }
}

function addPhotos() {
    global $config;

    $photos = $_FILES['photos'];
    $duantieubieu = $_POST['duantieubieu'];
    $thumbSize = Array('137-113','47-47');
    if($duantieubieu==1){
        $thumbSize = Array('137-113','47-47','107-106');
    }
    $upload_dir = $config ['upload_dir'].'/articles/'.date("Y").'/'.date("m");

    $now = time();

    for($i=0;$i<count($photos['name']);$i++) {
        if ((($photos["type"][$i] == 'image/gif') || ($photos["type"][$i] == 'image/jpeg')
            || ($photos["type"][$i] == 'image/png') || ($photos["type"][$i] == 'image/jpg')) && ($photos["size"][$i] < 5000000)) {
            $arrPhotoNameExplode = explode('.', $photos["name"][$i]);
            $photoExtention = $arrPhotoNameExplode[(count($arrPhotoNameExplode)-1)];
            $filename = $now.'-'.rand(0,999999).'.'.strtolower($photoExtention);
            if (!file_exists($upload_dir)) {
                if(!mkdir($upload_dir, 0777,true)) {
                    $_SESSION['rsMessages'] .= "<span class='error'>Can not create directory, please check permission.</span><br/>";
                }
            }
            if (move_uploaded_file($photos["tmp_name"][$i],$upload_dir."/".$filename)) {
                for($s=0;$s<count($thumbSize);$s++) {// start create thumbnail
                    $upload_dir_thumb = $upload_dir.'/thumb-'.$thumbSize[$s];
                    if (!file_exists($upload_dir_thumb)) {
                        if(!mkdir($upload_dir_thumb, 0777,true)) {
                            $_SESSION['rsMessages'] .= "<span class='error'>Can not create thumb directory, please check permission.</span><br/>";
                        }
                    }
                    $wh = explode('-',$thumbSize[$s]);
                    $thumbnail_width = $wh[0];
                    $thumbnail_heigh = $wh[1];

                    //$output_thumb_filename = $upload_dir_thumb.'/'.basename($filename);
                    include_once (BASE_PATH."/ext/phpThumb/ThumbLib.inc.php");
                    $options = array('resizeUp' => true, 'jpegQuality'=>88);
                    //$fileData = file_get_contents($upload_dir."/".$filename);
                    //$thumb = PhpThumbFactory::create($fileData, $options, true);
                    $thumb = PhpThumbFactory::create($upload_dir."/".$filename, $options);
                    $thumb->adaptiveResize($thumbnail_width+10, $thumbnail_heigh+10);
                    $thumb->cropFromCenter($thumbnail_width, $thumbnail_heigh);
                    $thumb->save($upload_dir_thumb.'/'.$filename,'jpg');
                }
                $_SESSION['rsMessages'] .= "<span class='success'>".$photos["name"][$i]." upload successful!.</span><br/>";
            }else {
                $_SESSION['rsMessages'] .= "<span class='error'>Error, Can not upload photo: ".$photos["name"][$i].'</span><br/>';
            }
        }else {
            $_SESSION['rsMessages'] .= "<span class='error'>Invalid file ".$photos['name'][$i].'</span><br/>';
        }
    }
    $_SESSION['rsMessages'] .= "<span class='success'>Add new photos completed!</span><br/>";
}


?>
